package com.lntea.redis;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Client;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.PipelineBlock;
import redis.clients.jedis.Response;

public class PipeliningDemo {
	Jedis jedis;
	
	@Before
	public void connect(){
		jedis = new Jedis("192.168.1.108", 6379);
		Client client = jedis.getClient();
		System.out.println(client.getHost()+":"+client.getPort());
	}
	
	@Test
	public void pipe(){
		Pipeline pipeline = jedis.pipelined();
		Client client = new Client("192.168.1.108", 6380);
		//pipeline.setClient(new Client("localhost"));
		//pipeline.set("key1", "value0");
		pipeline.set("key2", "value1");
		//pipeline.sadd("skey", "member1");
		Response<String> resp1 = pipeline.lindex("lkey", 0);
		pipeline.sync();
		System.out.println(resp1.get());
		
		//change client
		//before change client ,you should sync first.
		//or you will lose your previous operation
		pipeline.setClient(client);
		
		//Response<List<String>> resp2 = pipeline.hmget("user_info_1", "name","level");
		pipeline.set("user_name_1", "root");
		
		//syncronize pipeline by reading all responses
		pipeline.sync();
		//System.out.println(resp1.get());
		//System.out.println(resp2.get().toString());
		//syncronize pipeline and return all responses
		//List<Object> list = pipeline.syncAndReturnAll();
		//System.out.println(list.toString());
	}
	
	
	public void pipelineBlock(){
		//deprecated
		List<Object> list = jedis.pipelined(new PipelineBlock() {
			
			@Override
			public void execute() {
				System.out.println("pipeline block execute");
			}
		});
	}
}
